package ${table.pag}.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import ${table.pag}.entity.${table.className};
import ${table.pag}.dto.${table.className}Dto;
import ${table.pag}.service.${table.className}Service;

<#if swagger=='T'>
@Api(value = "Rest${table.className}Controller", description = "Rest${table.className}Controller接口")
</#if>
@RestController
@RequestMapping("/rest/${table.className?uncap_first}")
public class Rest${table.className}Controller extends BaseController {

	@Autowired
	private ${table.className}Service ${table.className?uncap_first}Service;

	<#if swagger=='T'>
	@ApiOperation(value="保存", notes="保存接口")
	</#if>
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("${table.className?uncap_first}Dto") ${table.className}Dto ${table.className?uncap_first}Dto) {
		${table.className} ${table.className?uncap_first} = new ${table.className}();
		BeanUtils.copyProperties(${table.className?uncap_first}Dto,${table.className?uncap_first});
		${table.className?uncap_first}.setId(${table.className?uncap_first}Service.getNewId());
		${table.className?uncap_first}Service.insert(${table.className?uncap_first});
		return ResultMessage.success();
	}

	<#if swagger=='T'>
	@ApiOperation(value="更新", notes="更新接口")
	</#if>
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("${table.className?uncap_first}Dto") ${table.className}Dto ${table.className?uncap_first}Dto) {
		${table.className} old${table.className} = ${table.className?uncap_first}Service.getById(${table.className?uncap_first}Dto.getId());
		if(old${table.className}==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		${table.className} ${table.className?uncap_first} = new ${table.className}();
		BeanUtils.copyProperties(${table.className?uncap_first}Dto,${table.className?uncap_first});
		${table.className?uncap_first}.setModifyTime(old${table.className}.getModifyTime());
		if (${table.className?uncap_first}Service.update(${table.className?uncap_first})>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	<#if swagger=='T'>
	@ApiOperation(value="列表", notes="分页查询接口")
	</#if>
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("${table.className?uncap_first}Dto") ${table.className}Dto ${table.className?uncap_first}Dto) {
		ResultMessage result = ResultMessage.success();
		${table.className} ${table.className?uncap_first} = new ${table.className}();
		BeanUtils.copyProperties(${table.className?uncap_first}Dto,${table.className?uncap_first});
		PageInfo<${table.className}> pageInfo = ${table.className?uncap_first}Service.page(${table.className?uncap_first},getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	<#if swagger=='T'>
	@ApiOperation(value="删除", notes="删除接口")
	</#if>
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		${table.className} ${table.className?uncap_first} = ${table.className?uncap_first}Service.getById(id);
		if(${table.className?uncap_first}==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(${table.className?uncap_first}Service.delete(${table.className?uncap_first})>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	<#if swagger=='T'>
	@ApiOperation(value="详情", notes="详情接口")
	</#if>
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		${table.className} ${table.className?uncap_first} = ${table.className?uncap_first}Service.getById(id);
		if(${table.className?uncap_first}==null)
			throw new BusinessException("无效的数据!");
		${table.className}Dto ${table.className?uncap_first}Dto = new ${table.className}Dto();
		BeanUtils.copyProperties(${table.className?uncap_first},${table.className?uncap_first}Dto);
		ResultMessage result = ResultMessage.success();
		result.setContent(${table.className?uncap_first}Dto);
		return result;
	}
}
